package net.wang.concurrency.test.syncContainer;

import lombok.extern.slf4j.Slf4j;
import net.wang.concurrency.annoations.NotThreadSafe;
import org.junit.Test;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;


@SuppressWarnings("ALL")
@NotThreadSafe
@Slf4j
public class VectorExample3 {

    public static Vector<Integer> list = new Vector<>();

    private void test1(Vector<Integer> vector) {
        for (Integer integer : vector) {
            if (integer == 3) {
                vector.remove(3);
            }
        }

    }

    private void test2(Vector<Integer> vector) {
        Iterator<Integer> iterator = vector.iterator();
        while (iterator.hasNext()) {
            Integer next = iterator.next();
            if (next == 3) {
                next.reverse(3);
            }
        }

    }

    private void test3(Vector<Integer> vector) {
        for (int i = 0; i < 3; i++) {
            if (i == 3) {
                vector.remove(3);
            }
        }

    }

    @Test
    public void notThreadSafeTest() throws InterruptedException {

        list.add(1);
        list.add(2);
        list.add(3);
//        test1(list); //error
//        test2(list); //error
        test3(list);
    }


}
